package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.InsuranceDetailsExport;
import com.ruoyi.system.domain.SysBusinessInfo;
import com.ruoyi.system.domain.UpdatePayStatusAndPayTime;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysInsuranceDetails;
import com.ruoyi.system.service.ISysInsuranceDetailsService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.StringUtils;

/**
 * 投保单下载Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-17
 */
@Service
public class SysInsuranceDetailsServiceImpl implements ISysInsuranceDetailsService 
{
    @Autowired
    private SysInsuranceDetailsMapper sysInsuranceDetailsMapper;
    @Autowired
    private SysBusinessInfoMapper sysBusinessInfoMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDealerInfoMapper sysDealerInfoMapper;

    /**
     * 查询投保单下载
     * 
     * @param id 投保单下载ID
     * @return 投保单下载
     */
    @Override
    public SysInsuranceDetails selectSysInsuranceDetailsById(Long id)
    {
        return sysInsuranceDetailsMapper.selectSysInsuranceDetailsById(id);
    }

    /**
     * 查询投保单下载列表
     * 
     * @param sysInsuranceDetails 投保单下载
     * @return 投保单下载
     */
    @Override
    public List<SysInsuranceDetails> selectSysInsuranceDetailsList(SysInsuranceDetails sysInsuranceDetails)
    {
        return sysInsuranceDetailsMapper.selectSysInsuranceDetailsList(sysInsuranceDetails);
    }

    @Override
    public List<SysInsuranceDetails> selectPaySysInsuranceDetailsList(SysInsuranceDetails sysInsuranceDetails)
    {
        return sysInsuranceDetailsMapper.selectPaySysInsuranceDetailsList(sysInsuranceDetails);
    }

    /**
     * 新增投保单下载
     * 
     * @param sysInsuranceDetails 投保单下载
     * @return 结果
     */
    @Override
    public int insertSysInsuranceDetails(SysInsuranceDetails sysInsuranceDetails)
    {
        sysInsuranceDetails.setCreateTime(DateUtils.getNowDate());
        return sysInsuranceDetailsMapper.insertSysInsuranceDetails(sysInsuranceDetails);
    }

    /**
     * 修改投保单下载
     * 
     * @param sysInsuranceDetails 投保单下载
     * @return 结果
     */
    @Override
    public int updateSysInsuranceDetails(SysInsuranceDetails sysInsuranceDetails)
    {
        sysInsuranceDetails.setUpdateTime(DateUtils.getNowDate());
        return sysInsuranceDetailsMapper.updateSysInsuranceDetails(sysInsuranceDetails);
    }

    @Override
    public int updateLossPolicyAndInsuranceStartdate(Long id, String lossPolicy, String insuranceStartdate){
        try{
            SysInsuranceDetails sysInsuranceDetails = new SysInsuranceDetails();
            sysInsuranceDetails.setId(id);
            sysInsuranceDetails.setLossPolicy(lossPolicy);
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            sysInsuranceDetails.setPayStatus("3");
            sysInsuranceDetails.setInsuranceStartdate(insuranceStartdate);
            sysInsuranceDetails.setUpdateTime(DateUtils.getNowDate());
            return sysInsuranceDetailsMapper.updateSysInsuranceDetails(sysInsuranceDetails);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updatePayStatusAndPayTime(List<UpdatePayStatusAndPayTime> updatePayStatusAndPayTimeList) {
        try{
            if(updatePayStatusAndPayTimeList!=null && !updatePayStatusAndPayTimeList.isEmpty()){
                for(UpdatePayStatusAndPayTime updatePayStatusAndPayTime:updatePayStatusAndPayTimeList){
                    SysInsuranceDetails sysInsuranceDetails = new SysInsuranceDetails();
                    sysInsuranceDetails.setId(updatePayStatusAndPayTime.getId());
                    sysInsuranceDetails.setPayStatus(updatePayStatusAndPayTime.getPayStatus());
                    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
                    sysInsuranceDetails.setPayTime(updatePayStatusAndPayTime.getPayTime());
                    sysInsuranceDetails.setUpdateTime(DateUtils.getNowDate());

                    sysInsuranceDetailsMapper.updateSysInsuranceDetails(sysInsuranceDetails);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 删除投保单下载对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysInsuranceDetailsByIds(String ids)
    {
        return sysInsuranceDetailsMapper.deleteSysInsuranceDetailsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除投保单下载信息
     * 
     * @param id 投保单下载ID
     * @return 结果
     */
    @Override
    public int deleteSysInsuranceDetailsById(Long id)
    {
        return sysInsuranceDetailsMapper.deleteSysInsuranceDetailsById(id);
    }

    @Override
    public List<InsuranceDetailsExport> selectInsuranceDetailsExportList(Long id) {
        List<InsuranceDetailsExport> list = new ArrayList<>();
        try{
            List<SysBusinessInfo> sysBusinessInfoList = sysBusinessInfoMapper.selectSysBusinessInfoListByinsuranceDetailsId(id);
            if(sysBusinessInfoList!=null && !sysBusinessInfoList.isEmpty()){
                for(SysBusinessInfo sysBusinessInfo:sysBusinessInfoList){
                    InsuranceDetailsExport insuranceDetailsExport = new InsuranceDetailsExport();
                    insuranceDetailsExport.setCompensationLimit(sysBusinessInfo.getCompensationLimit());
                    insuranceDetailsExport.setCarBrandAndBrandModel(sysBusinessInfo.getCarBrand() + sysBusinessInfo.getBrandModel());
                    insuranceDetailsExport.setCarNumberAndEngineNo(sysBusinessInfo.getCarNumber());
                    insuranceDetailsExport.setVinNo(sysBusinessInfo.getVinNo());

                    String dealerName = sysDeptMapper.selectDeptById(sysUserMapper.selectUserById(Long.valueOf(sysBusinessInfo.getCreateBy())).getDeptId()).getDeptName();
                    insuranceDetailsExport.setDealerName(dealerName);
                    insuranceDetailsExport.setUnifiedSocialCode(sysDealerInfoMapper.selectunifiedSocialCodeByDealerName(dealerName));
                    list.add(insuranceDetailsExport);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
